package org.xc.uvc_camera;

import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class FileUtil {

    public static final String BASE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator  + "666";

    static {
        File rootFile = new File(BASE_PATH);
        if (!rootFile.exists()) {
            rootFile.mkdirs();
        }
    }

    public static String createFile(String fileName) {
        File file = new  File(BASE_PATH,fileName);
        if (file.exists()) {
            file.delete();
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file.getAbsolutePath();
    }

    public static File getFile(String fileName) {
        if (TextUtils.isEmpty(fileName)) {
            return null;
        }
        File file = new File(BASE_PATH,fileName);
        if (!file.exists()) {
            return null;
        }
        return file;
    }

    /**
     * type 7-前七 8-后八
     */
    public static List<File> getMp4Files(String currFileName,int type) {
        File rootFile = new File(BASE_PATH);
        File[] mp4Files = rootFile.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File file, String s) {
                return s.endsWith("mp4");
            }
        });
        //排序
        Arrays.sort(mp4Files, new Comparator<File>() {
            @Override
            public int compare(File file, File t1) {
                long temp = Long.valueOf(file.getName().split("\\.")[0]);
                long temp1 = Long.valueOf(t1.getName().split("\\.")[0]);
                if (temp < temp1) {
                    return -1;
                } else if (temp > temp1) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });

        //上传时间点
        long curr = Long.valueOf(currFileName);
        //离上传时间点最近的文件名
        long tempL = 9999999999999999l;
        int position = -1;
        for(int i = 0; i < mp4Files.length; i ++) {
            //该文件时间戳
            long temp = Long.valueOf(mp4Files[i].getName().split("\\.")[0]);
            long calc = Math.abs(curr - temp);
            if (calc <= tempL) {
                tempL = temp;
                position = i;
            }
        }

        //取得视频
        List<File> files = new ArrayList<>();
        if (type == 7) {
            //前七个
            int temp = 7;
            do {
                files.add(mp4Files[position]);
                position --;
                temp --;
            } while (position >= 0 && temp >= 0);
        } else if (type == 8) {
            int temp = 8;
            do {
                files.add(mp4Files[position]);
                position ++;
                temp --;
            } while (position < mp4Files.length && temp >= 0);
        }
        return files;
    }
}
